﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class RecyclePage : Page
    {
        public UITriggerTab breakupTab;
        public UITriggerTab resetTab;

        public RecycleBreakupPanel breakupPanel;
        public RecycleResetPanel resetPanel;

        public List<UITweener> tweeners;
        public List<UITweener> breakupTweeners;
        public List<UITweener> resetTweeners;

        public bool isPlaying = false;

        public const string wheelAniNormalName = "huishouwu_donghua";
        public const string wheelAniSpeedupName = "zhuanpan_jiasu";

        public const string pageAudio = "agil_com_0016";

        int tabIndex = 0;
        protected override void DoOpen()
        {
            if (options.ContainsKey("type"))
            {
                if (options["type"] == "0")
                {
                    tabIndex = 0;
                }
                else if (options["type"]== "1")
                {
                    tabIndex = 1;
                }
            }
            else
            {
                tabIndex = 0;
            }

            OpenSubContent();
            foreach (var tweener in tweeners)
            {
                if (tweener != null)
                {
                    tweener.ResetToBeginning();
                    tweener.PlayForward();
                }
            }
            if (tabIndex == 0)
            {
                foreach (var tweener in breakupTweeners)
                {
                    if (tweener != null)
                    {
                        tweener.ResetToBeginning();
                        tweener.PlayForward();
                    }
                }
            }
            else if (tabIndex == 1)
            {
                foreach (var tweener in resetTweeners)
                {
                    if (tweener != null)
                    {
                        tweener.ResetToBeginning();
                        tweener.PlayForward();
                    }
                }
            }

           
           // AudioManager.Instance.PlayUIAudioChannel("agil_com_0016");
            
        }

        public override void DoReopen()
        {
            breakupPanel.ReShowTab();
            resetPanel.ReShowTab();
        }

        protected override void DoClose()
        {
            breakupPanel.OnPageClose();
            resetPanel.OnPageClose();
        }

        public override void OnCoverPageRemove()
        {
            breakupPanel.OnRemoveCoverPage();
            resetPanel.OnRemoveCoverPage();
        }

        public void OnBreakupTabClick()
        {
            if (tabIndex== 0)
            {
                return;
            }
            tabIndex = 0;
            OpenSubContent();

            StaticData.ClearRecycleBreakupSelect();
            StaticData.ClearRecycleResetSelect();
        }

        public void OnResetTabClick()
        {
            if (tabIndex == 1)
            {
                return;
            }
            tabIndex = 1;
            OpenSubContent();

            StaticData.ClearRecycleBreakupSelect();
            StaticData.ClearRecycleResetSelect();
        }

        void OpenSubContent()
        {
            List<UITweener> content_tweeners = new List<UITweener>();
            switch (tabIndex)
            {
                case 0:
                    breakupTab.Trigger();
                    resetTab.UnTrigger();
                    breakupPanel.ShowTab();
                    resetPanel.CloseTab();
                    content_tweeners = breakupPanel.tweeners;
                    break;
                case 1:
                    breakupTab.UnTrigger();
                    resetTab.Trigger();
                    breakupPanel.CloseTab();
                    resetPanel.ShowTab();
                    content_tweeners = resetPanel.tweeners;
                    break;
            }
            float tweenerTime = 0;
            foreach (var tweener in content_tweeners)
            {
                tweener.ResetToBeginning();
                tweener.PlayForward();
                if (tweenerTime < tweener.delay + tweener.duration)
                {
                    tweenerTime = tweener.delay + tweener.duration;
                }
            }
            isPlaying = true;
            Invoke("OnOpenSubPageTweenOver", tweenerTime);
        }

        void OnOpenSubPageTweenOver()
        {
            switch (tabIndex)
            {
                case 0:
                    breakupPanel.OnOpenSubPageTweenOver();
                    break;
                case 1:
                    resetPanel.OnOpenSubPageTweenOver();
                    break;
            }
        }


        public void OnMemoryShopButtonClick()
        {
            PageManager.Instance.OpenPage("ShopPage", "type=" + (int)ShopType.MemoryShop);
        }

        public void OnDetailButtonClick()
        {
            PageManager.Instance.OpenPage("DescriptionPage", "");
            var page = UI.PageManager.Instance.currentPage as UI.DescriptionPage;
            if (page != null)
            {

                page.SetText("recycle_desc_text");
                page.SetStyle(DescriptionPage.DesType.Recycle);
            }

        }


    }
}